package org.example.infrastructure.adapter.repository;

import org.example.domain.strategy.model.entity.StrategyAwardEntity;
import org.example.domain.strategy.model.entity.StrategyRuleEntity;
import org.example.domain.strategy.repository.IStrategyRuleRepository;
import org.example.infrastructure.dao.IStrategyRuleDao;
import org.example.infrastructure.dao.po.StrategyRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class StrategyRuleRepository implements IStrategyRuleRepository {
    @Autowired
    IStrategyRuleDao iStrategyRuleDao;
    @Override
    public List<StrategyRuleEntity> queryStrategyRuleEntityByStrategyId(Long strategyId) {
        List<StrategyRule> strategyRuleList = iStrategyRuleDao.queryStrategyRuleListByStrategyId(strategyId);
        List<StrategyRuleEntity> res = new ArrayList<>(strategyRuleList.size());
        for (StrategyRule strategyRule: strategyRuleList) {
            StrategyRuleEntity strategyRuleEntity = StrategyRuleEntity.builder()
                    .strategyId(strategyRule.getStrategyId())
                    .awardId(strategyRule.getAwardId())
                    .ruleType(strategyRule.getRuleType())
                    .ruleModel(strategyRule.getRuleModel())
                    .ruleValue(strategyRule.getRuleValue())
                    .build();
            res.add(strategyRuleEntity);
        }
        return res;
    }

    @Override
    public String queryRuleValue(Long strategyId, Long awardId, String ruleModel) {
        StrategyRule rule = StrategyRule.builder()
                .strategyId(strategyId)
                .awardId(awardId)
                .ruleModel(ruleModel)
                .build();
        return iStrategyRuleDao.queryRuleValue(rule);
    }

    @Override
    public List<StrategyRuleEntity> queryStrategyRuleEntityForModel(Long strategyId, Long awardId, String ruleModel) {
        StrategyRule rule = StrategyRule.builder()
                .strategyId(strategyId)
                .awardId(awardId)
                .ruleModel(ruleModel)
                .build();
        List<StrategyRule> strategyRuleList = iStrategyRuleDao.queryStrategyRuleListForModel(rule);
        List<StrategyRuleEntity> res = new ArrayList<>(strategyRuleList.size());
        for (StrategyRule strategyRule: strategyRuleList) {
            StrategyRuleEntity strategyRuleEntity = StrategyRuleEntity.builder()
                    .ruleModel(strategyRule.getRuleModel())
                    .ruleValue(strategyRule.getRuleValue())
                    .build();
            res.add(strategyRuleEntity);
        }
        return res;
    }
}
